# 哪些公共基本操作方法，初始化应该有操作 方法
import datetime
import os

from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select

class BaseAction(object):
    # :后面是 数据类型，这个driver就是浏览器
    def __init__(self, driver):
        # 进行浏览器操作，初始化一 定有浏览器
        self.driver = driver

    # 发现元素方法封装，加上等待步骤
    def find_element(self, location, timeout=10, poll_frequency=1):
        #  By.ID类型 ，值，加上智能等待
        local_by, local_value = location
        wait = WebDriverWait(self.driver, timeout, poll_frequency)
        return wait.until(lambda driver: self.driver.find_element(local_by, local_value))

    def find_elements(self, location, timeout=10, poll_frequency=1):
        #  By.ID类型 ，值，加上智能等待
        local_by, local_value = location
        wait = WebDriverWait(self.driver, timeout, poll_frequency)
        return wait.until(lambda driver: self.driver.find_elements(local_by, local_value))

    #     再封装一下输入文本方法
    def input(self, location, text):
        # 使用我们 封装方法找到元素
        ele = self.find_element(location)
        # 点击元素
        ele.click()
        # 清除元素内 文本信息
        self.driver.execute_script("arguments[0].value = '';", ele)
        # 输入文本信息
        ele.send_keys(text)


    def click(self, location):
        self.find_element(location).click()

    # 鼠标悬停
    def actions(self, location):
        actions = ActionChains(self.driver)
        actions.move_to_element(self.find_element(location)).perform()

    # 滚动到指定位置
    def execute_script_1(self, loc):
        self.driver.execute_script("window.scrollTo(0,%s);" % loc)

    def get_text(self, location):
        element = self.find_element(location)
        return element.text

    #使用函数execute_script执行js脚本滚动到特定元素
    def execute_script_2(self, location):
        element = self.find_element(location)
        self.driver.execute_script("arguments[0].scrollIntoView();", element)

    # 选择下拉框  仅用于<select>标签
    def select_by_value(self, location, value):
        select = Select(self.find_element(location))
        select.select_by_value(value)

    def select_by_index(self, location, index):
        select = Select(self.find_element(location))
        select.select_by_index(index)
